(function() {
	'use strict';

	CCR.Models.Lrc = Backbone.Model.extend({
		options: {
	            lrcid : "#lrcPane",
	            lrcTag: {
	                sname: "ti",
	                cname: "cl",
	                qname: "cs",
	                bname: "ps",
	                sgname: "ar",
	                special: "al",
	                kname: "by",
	                end: "end"
	            },
	            lrcClassV1: "normalLyric",
	            lrcClassV2: "currentLyric",
	            lrcClassV3: "v3",
	            lrcEmpty: 20,
	            lrcCenter: 30,
	            lrc_lines: []
	        },
	        lrcRead : function (lrc, fun) {
	            if (!lrc) return;
	            if (fun != null && typeof fun == "function")
	                return fun.call(this, lrc);
	            var jsons = {};
	            jsons["lrcs"] = new Array();
	            var lrs = lrc.split('[');
	            var lejt = lrs.length;
	            for (var i = 0; i < lejt; i++) {
	                var element = lrs[i];
	                var dic = element.split(']');
	                if (dic.length == 2)
	                    if (dic[0].search("^[0-9]{2}:[0-9]{2}.[0-9]{2}$") < 0) {

	                        var title = dic[0].split(':');
	                        if (title.length === 2)
	                            switch (title[0]) {
	                            	case this.options.lrcTag.cname: jsons[this.options.lrcTag.sname] = title[1]; break;
	                                case this.options.lrcTag.cname: jsons[this.options.lrcTag.cname] = title[1]; break;
	                                case this.options.lrcTag.qname: jsons[this.options.lrcTag.qname] = title[1]; break;
	                                case this.options.lrcTag.bname: jsons[this.options.lrcTag.bname] = title[1]; break;
	                                case this.options.lrcTag.sgname: jsons[this.options.lrcTag.sgname] = title[1]; break;
	                                case this.options.lrcTag.special: jsons[this.options.lrcTag.special] = title[1]; break;
	                                case this.options.lrcTag.kname: jsons[this.options.lrcTag.kname] = title[1]; break;
	                                case this.options.lrcTag.end: jsons[this.options.lrcTag.end] = title[1]; break;
	                            }
	                    } else {
	                        var lt = { "time": dic[0], "name": dic[1] };
	                        jsons["lrcs"].push(lt);
	                    }
	            }
	            for(i in jsons){
	            	this[i] = jsons[i];
	            }
	            return jsons;
	        },
	        lrcLoad : function (json, fun) {
	            if (!json) {
	                $(this.options.lrcid).html("<li>无法搜索到歌词</li>");
	                return;
	            };
	            var sname = json[this.options.lrcTag.sname];
	            var cname = json[this.options.lrcTag.cname];
	            var qname = json[this.options.lrcTag.qname];
	            var bname = json[this.options.lrcTag.bname];
	            var sgname = json[this.options.lrcTag.sgname];
	            var special = json[this.options.lrcTag.special];
	            var kname = json[this.options.lrcTag.kname];
	            var lrcs = json["lrcs"];
	            var end = json[this.options.lrcTag.end];
	            $(this.options.lrcid).html("");
	            this.options.lrc_lines = new Array();
	            if (sname) sname["time"] ? this.addLrc(this.options.lrcClassV1, sname["name"], sname["time"]) : this.addLrc(this.options.lrcClassV1, sname);
	            if (cname) cname["time"] ? this.addLrc(this.options.lrcClassV1, cname["name"], cname["time"]) : this.addLrc(this.options.lrcClassV1, cname);
	            if (qname) qname["time"] ? this.addLrc(this.options.lrcClassV1, qname["name"], qname["time"]) : this.addLrc(this.options.lrcClassV1, qname);
	            if (bname) bname["time"] ? this.addLrc(this.options.lrcClassV1, bname["name"], bname["time"]) : this.addLrc(this.options.lrcClassV1, bname);
	            if (sgname) sgname["time"] ? this.addLrc(this.options.lrcClassV1, sgname["name"], sgname["time"]) : this.addLrc(this.options.lrcClassV1, sgname);
	            if (special) special["time"] ? this.addLrc(this.options.lrcClassV1, special["name"], special["time"]) : this.addLrc(this.options.lrcClassV1, special);
	            if (kname) kname["time"] ? this.addLrc(this.options.lrcClassV1, kname["name"], kname["time"]) : this.addLrc(this.options.lrcClassV1, kname);
	            if (lrcs)
	                for (var index in lrcs) {
	                    this.addLrc(this.options.lrcClassV1, lrcs[index]["name"], lrcs[index]["time"]);
	                }
	            if (end) end["time"] ? this.addLrc(this.classV1, end["end"], end["time"]) : this.addLrc(this.options.lrcClassV1, end);
	            for (var it = 0; it < this.options.lrcEmpty; it++) {
	                this.addLrc("", "&nbsp;", "");
	            }
	            if (typeof fun == 'function')
	                fun.call(null, null);
	            if ($(this.options.lrcid).children().length == 0) {
	                $(this.options.lrcid).html("<li>未找到相关歌词</li>");
	            };
	        },
	        addLrc : function (cla, text, val) {
	            var lrc_line = $("<li></li>");
	            if(text.replace(" ","").replace(/\n+/,"").replace("&nbsp;","").length == 0) return;
	            lrc_line.html(text ? text : "");
	            lrc_line.attr("class", cla ? cla : "");
	            lrc_line.attr("lang", val ? val : "");
	            $(this.options.lrcid).append(lrc_line);
	            this.options.lrc_lines.push(lrc_line);
	        },
	        getLrcByValue : function (val) {
	            var le = this.options.lrc_lines.length;
	            val = "^" + val + ".*";
	            for (var i = 0; i < le; i++) {
	                if (this.options.lrc_lines[i].lang.search(val) == 0) return this.options.lrc_lines[i];
	            }
	            return null;
	        },
	        getLrcByValueInd : function (val) {
	            var le = this.options.lrc_lines.length - 1;
	            for (var i = le; i >= 0; i--) {
	                var p = this.options.lrc_lines[i][0].lang;
	                if (p)
	                    if (p < val) return this.options.lrc_lines[i][0];
	            }
	            return null;
	        },
	        lrcTorun : function (time) {
	            var time = this.lrctoTimer(Math.round(time * 100) / 100);
	            var line = this.getLrcByValueInd(time);
	            if (line)
	                if (this.upkp != line) {
	                    $(line).attr("class", this.options.lrcClassV2);
	                    this.lrcUpTop(line);
	                    if (this.upkp) $(this.upkp).attr("class", this.options.lrcClassV1);
	                    this.upkp = line;
	                }
	        },
	        lrcUpTop : function (obj) {
	            var thisTop = obj.offsetTop;
	            if (!$(this.options.lrcid).length)
	                return;
	            var st = $(this.options.lrcid).get(0).scrollTop;
	            var nextTop = thisTop - this.options.lrcCenter;
	            this.lrcmyf($(this.options.lrcid).get(0), st, nextTop);
	        },
	        lrcmyf : function (obj, f, m) {
	            if (this.options.lrcTimer != null) {
	                clearTimeout(this.options.lrcTimer);
	            }
	            this.isUpOrDown(obj, f, m);
	        },
	        isUpOrDown : function (obj, f, m) {
	      	  $(obj).css("top",-m+"px");
	      	  //$(obj).animate({scrollTop:m},300);
	        },
	        lrctoTimer : function (time) {
	            var m, s;
	            m = Math.floor(time / 60);
	            m = isNaN(m) ? '--' : (m >= 10) ? m : '0' + m;
	            s = Math.floor(time % 60);
	            s = isNaN(s) ? '--' : (s >= 10) ? s : '0' + s;
	            return m + ':' + s;
	        },
	});
})();



